package com.wshl.aijiuunion.controller.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.hjq.permissions.OnPermission;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.wshl.aijiuunion.BaseActivity;
import com.wshl.aijiuunion.GlobConstant;
import com.wshl.aijiuunion.NetUrl;
import com.wshl.aijiuunion.R;
import com.wshl.aijiuunion.utils.DialogUtil;
import com.wshl.aijiuunion.utils.GlideUtils;
import com.wshl.aijiuunion.utils.imgdown.ImageDownload;
import com.wshl.aijiuunion.view.widget.NofastClickTextview;
import com.wshl.aijiuunion.wxapi.Constancts;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created buy 风凊扬 on 2019/11/1 0001
 */
public class TujianActivity extends BaseActivity implements DialogUtil.OnBottomDilogClick {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.tv_tuijianma)
    TextView tvTuijianma;
    @BindView(R.id.img_erweima)
    ImageView imgErweima;
    @BindView(R.id.rl_tuijianma)
    RelativeLayout rlTuijianma;
    @BindView(R.id.tv_tuijian)
    NofastClickTextview tvTuijian;
    private IWXAPI wxApi;
    private String code;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_tuijian;
    }

    private String url = "http://cncttv.com/uploads/avatar/mfgQZ9XUHMXEZ8FbMO9uG3lAjW1NbImJdYhHGLrK.jpeg";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        tvTopTitle.setText("推荐好友");
        String cover = SPUtils.getInstance().getString(GlobConstant.RECOMMENCOVER);
        code = SPUtils.getInstance().getString(GlobConstant.RECOMMENDCODE);

        tvTuijianma.setText(code);
        GlideUtils.loadImage(this, NetUrl.IMG + cover, imgErweima);
        rlTuijianma.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                //保存到本地
                savePic();
                return true;
            }
        });

        DialogUtil.setOnBottomDilogClick(this);
    }

    @OnClick({R.id.img_back, R.id.tv_tuijian})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_tuijian://分享
                DialogUtil.showBottomDialog(TujianActivity.this, "微信好友", "朋友圈", "", "#57BE6A", "#57BE6A", "#57BE6A");

                break;
        }
    }

    private String info = "发现一款好玩的APP,赶快下载试试吧";

    private void share(int position) {
        //实例化
        wxApi = WXAPIFactory.createWXAPI(TujianActivity.this, Constancts.WXAPPID);
        wxApi.registerApp(Constancts.WXAPPID);
        WXWebpageObject webpage = new WXWebpageObject();

        webpage.webpageUrl = NetUrl.WXSHARE + code;
        final WXMediaMessage msg = new WXMediaMessage(webpage);
        if (position == 0) {
            msg.title = "艾灸联盟";
            msg.description = info;
        }
        if (position == 1) {
            msg.title = info;
            msg.description = info;
        }
        Bitmap thumb = BitmapFactory.decodeResource(getResources(), R.drawable.share_logo);
        msg.setThumbImage(thumb);
//            byte[] bytes = bitmap2Bytes(thumb, 28);
//            msg.thumbData = bytes;
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        req.message = msg;
        req.scene = position == 0 ? SendMessageToWX.Req.WXSceneSession : SendMessageToWX.Req.WXSceneTimeline;
        wxApi.sendReq(req);
    }


    private void savePic() {
        XXPermissions.with(this)
                .constantRequest() //设置被拒绝后继续申请，直到用户授权或者永久拒绝
                // .permission(Permission.SYSTEM_ALERT_WINDOW, Permission.REQUEST_INSTALL_PACKAGES) //支持请求6.0悬浮窗权限8.0请求安装权限
                .permission(Permission.Group.STORAGE) //不指定权限则自动获取清单中的危险权限
                .request(new OnPermission() {
                    @Override
                    public void hasPermission(List<String> granted, boolean isAll) {
                        // File downloadDir = new File(Environment.getExternalStorageDirectory(), "xinchewang");
                        new ImageDownload().onDownLoad(TujianActivity.this, url, GlobConstant.DEFAULT_CACHE_DIR);
                    }

                    @Override
                    public void noPermission(List<String> denied,
                                             boolean quick) {
                        //如果权限被永久禁止,quick值为true
                        //让用户手动打开权限设置
                        XXPermissions.gotoPermissionSettings(TujianActivity.this);
                    }
                });


    }

    @Override
    public void setTitle1(String title) {

        share(0);
    }

    @Override
    public void setTitle2(String title) {

        share(1);
    }

    @Override
    public void setTitle3(String title) {

    }
}
